
<?php
class H_View_Helper_Ckfinder extends Zend_View_Helper_Abstract{
    const FILE = "Files:/";
    const IMAGE = "Images:/";  
   
    public $view;
    public function ckfinder($name,$type = "Images:/",$value = null,$show = true,$attribs = null){
        $output = "";
        /*<input id="xImagePath" name="ImagePath" type="text" size="60" />
	<input type="button" value="Browse Server" onclick="BrowseServer( 'Images:/aaaa/bbbbb', 'xImagePath' );" />

        <input id="xFilePath" name="FilePath" type="text" size="60" />
	<input type="button" value="Browse Server" onclick="BrowseServer( 'Files:/', 'xFilePath' );" />
         *
         * /userfiles/images/(trangchu/khuvuca)/happyland.jpg
         * */
        $relOb = "ckfinfer_image_$name";
        if ($show && $type == self::IMAGE&&!empty ($value)){
            $imageFullPath = BASE_PATH .$value;            
            if (is_file($imageFullPath)){
                $thumbs = str_replace("/userfiles/", "/userfiles/_thumbs/", $value);
                $output.= "<div id='$relOb'><img src=\"$thumbs\" width=\"100px\" /></div>";
            }else{
                $output.= "<div id='$relOb'><span style='color:red'>".$this->view->t('File hinh khong ton tai')."</span></div>";
            }
        }else{
            $output.= "<div id='$relOb'><img src=\"$thumbs\" width=\"100px\" /></div>";
        }
        if ($type == self::FILE){
            $tmp = $this->view->t('Upload File Ckfinder');
        }elseif ($type == self::IMAGE){
            $tmp = $this->view->t('Upload Image Ckfinder');
        }
        $tmpFolder = $this->getFolder($value);
        $output.= $this->view->formText($name,$value,$attribs);
        $output.= "<button type=\"button\" onclick=\"CMS.CKFinder.BrowseServer( '".$type.$tmpFolder."', '".$name."','$relOb' );\">$tmp</button>
        <button type=\"button\" onclick=\"CMS.CKFinder.Delete( '$relOb','$name' );\">".$this->view->t('xoa')."</button>";
        return $output;

    }

    public function setView(Zend_View_Interface $view) {
		$this->view = $view;
    }

    private function getFolder($value){
        $pattern = "/\/[^\/]+\/[^\/]+\/(.*)\/.*/i";
        preg_match($pattern, $value, $matches);
        return $matches[1];
    }
}